<%--
  Created by IntelliJ IDEA.
  User: Fmc
  Date: 2022/6/29
  Time: 9:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>修改用户个人信息</title>
        <link rel="stylesheet" media="all" href="${pageContext.request.contextPath}/static/layui/css/layui.css">
        <script src="${pageContext.request.contextPath}/static/layui/layui.js"></script>
    </head>
    <body>
        <div style="width: 1000px;margin-top: 30px;">
            <form class="layui-form" action="" id="saveUserForm" lay-filter="saveUserFormFilter">
                <div class="layui-form-item">
                    <label class="layui-form-label">用户Id</label>
                    <div class="layui-input-inline">
                        <input type="text" name="userid" required lay-verify="required" readonly="readonly" placeholder="用户名id"
                               value="" class="layui-input" style="background-color: #eeeeee;">
                    </div>
                    <label class="layui-form-label">部门名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="deptid" disabled value="" class="layui-input" style="background-color: #eeeeee;">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="username" required lay-verify="required" value="" class="layui-input">
                    </div>
                    <label></label>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">电话</label>
                    <div class="layui-input-inline">
                        <input type="text" name="phonenumber" value="" class="layui-input">
                    </div>
                    <label></label>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-inline">
                        <input type="text" name="email" value="" class="layui-input">
                    </div>
                    <label></label>
                </div>
                <input type="hidden" id="avatar" name="avatar" value="">
                <div class="layui-form-item">
                    <label class="layui-form-label">头像</label>
                    <div class="layui-upload">
                        <img class="layui-upload-img" id="demo0" style="width: 100px;" src="">
                        </br>
                        <button type="button" class="layui-btn" id="test3" style="margin-left: 110px;">修改图片</button>
                        <div class="layui-upload-list" style="margin-left: 110px;">
                            <p id="demoText3"></p>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" >
                    <div class="layui-input-inline" style="width: 100%;text-align: center">
                        <button class="layui-btn" lay-submit lay-filter="formSaveUser">保存</button>
                    </div>
                </div>
            </form>
        </div>
    </body>
    <script>
        layui.use(['upload','jquery','form'],function (){
            var upload = layui.upload;
            var $ = layui.jquery;
            var form = layui.form;
            form.val('saveUserFormFilter', {
                "userid": "${userInfo.userid}"
                , "deptid": "${userInfo.dept_name}"
                , "username": "${userInfo.username}"
                , "email": "${userInfo.email}"
                , "phonenumber": "${userInfo.phonenumber}"
                , "sex": "${userInfo.sex}"
                , "avatar": "${userInfo.avatar}"
            })
            $('#demo0').attr('src', "${userInfo.avatar}");
            // 添加的提交按钮
            var submitBtn = $("button[lay-filter='formSaveUser']");
            // 初始化为失效状态
            submitBtn.attr("disabled","disabled").attr("style","background-color: #808080");
            // 用户名校验
            $("#saveUserForm input[name='username']").on('blur',function (){
                var username = $("#saveUserForm input[name='username']");
                if (username.val() === ""){
                    username.parent().next().html("用户名不能为空").attr("style","color: #ff0000;line-height: 38px");
                }else {
                    // 唯一性校验
                    console.log(username.val());
                    $.ajax({
                        url: '${pageContext.request.contextPath}/user/findNoSameName',
                        data: {
                            username: username.val(),
                            userid: "${userInfo.userid}"
                        },
                        dataType: 'JSON',
                        type: 'GET',
                        success: function (redata) {
                            if (redata.code == 0){
                                username.parent().next().html("√").attr("style","color: #00ff00;line-height: 38px");
                                submitBtn.removeAttr("disabled").removeAttr("style","background-color: #808080");
                            }else {
                                username.parent().next().html("用户名重复，请更换").attr("style","color: #ff0000;line-height: 38px");
                            }
                        }
                    });
                }
            });
            // 邮箱校验
            $("#saveUserForm input[name='email']").on('blur',function (){
                var email = $("#saveUserForm input[name='email']");
                var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
                if (email.val() === ""){
                    email.parent().next().html("邮箱不能为空").attr("style","color: #ff0000;line-height: 38px");
                }else if(!reg.test(email.val())){
                    email.parent().next().html("请填写正确的邮箱格式!~").attr("style","color: #ff0000;line-height: 38px");
                }else{
                    email.parent().next().html("√").attr("style","color: #00ff00;line-height: 38px");
                    submitBtn.removeAttr("disabled").removeAttr("style");
                }
            });
            // 电话校验
            $("#saveUserForm input[name='phonenumber']").on('blur',function (){
                var phonenumber = $("#saveUserForm input[name='phonenumber']");
                var reg = new RegExp("^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$");
                if (phonenumber.val() === ""){
                    phonenumber.parent().next().html("电话不能为空").attr("style","color: #ff0000;line-height: 38px");
                }else if(!reg.test(phonenumber.val())){
                    phonenumber.parent().next().html("请填写正确的电话格式!~").attr("style","color: #ff0000;line-height: 38px");
                }else{
                    phonenumber.parent().next().html("√").attr("style","color: #00ff00;line-height: 38px");
                    submitBtn.removeAttr("disabled").removeAttr("style");
                }
            });
            //常规使用 - 普通图片上传
            upload.render({
                elem: '#test3'
                ,url: '${pageContext.request.contextPath}/user/upload'
                ,field: 'xxx'
                ,done: function(res) {
                    // 如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    // 上传成功的一些操作
                    $('#avatar').val(res.data.src);
                    $('#demo0').attr('src', res.data.src); //图片链接（base64）
                    submitBtn.removeAttr("disabled").removeAttr("style","background-color: #808080");
                },
                error: function() {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText3');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function() {
                        uploadInst.upload();
                    });
                }
            });
            // 保存修改后的信息
            form.on('submit(formSaveUser)', function (data) {
                $.ajax({
                    url: '${pageContext.request.contextPath}/user/updateOneself'
                    , data: {
                        "userid": data.field.userid
                        ,"username": data.field.username
                        ,"email": data.field.email
                        ,"phonenumber": data.field.phonenumber
                        ,"sex": data.field.sex
                        ,"avatar": data.field.avatar
                    }
                    , dataType: "json"
                    , type: "post"
                    , success: function (redata) {
                        if (redata.code == 0) {
                            console.log(redata)
                            layer.msg(redata.msg + "。右上角信息将在下次登录时展示。");
                        } else {
                            layer.msg(redata.msg);
                        }
                    }
                });
                return false;
            });
        });
    </script>
</html>
